//
//  DisplayUnit.m
//  countDownTimer
//
//  Created by fanxiao on 16/2/25.
//  Copyright © 2016年 尚宇电子. All rights reserved.
//

#import "DisplayUnit.h"
@interface DisplayUnit()
@property (nonatomic, strong) UILabel* digitalLB;
@end

@implementation DisplayUnit

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0,0, UnitW, UnitH)];
        backView.backgroundColor = [UIColor blackColor];
        [self addSubview:backView];
        
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5,5, UnitW - 2 * 5, UnitH-2*5)];
        [backView addSubview:scrollView];
        self.digitalLB = [[UILabel alloc] initWithFrame:CGRectMake(0,0, UnitW - 2 * 5, UnitH-2*5)];
        self.digitalLB.textColor = [UIColor redColor];
        self.digitalLB.textAlignment = NSTextAlignmentCenter;
        self.digitalLB.text = @"0";
        self.digitalLB.font = [UIFont boldSystemFontOfSize:20];
        [scrollView addSubview:self.digitalLB];
        
        self.digitalLB.backgroundColor = [UIColor blackColor];
    }
    return self;
}


-(void)updateDigitalLBWithCount:(NSInteger)count type:(DisplayUnitType)type{

    //如果相同，不做动画
    NSString *countStr = [NSString stringWithFormat:@"%ld",count];
    if ([self.digitalLB.text isEqualToString:countStr]) {
        self.digitalLB.text = [NSString stringWithFormat:@"%ld",count];
        self.digitalLB.transform = CGAffineTransformIdentity;
        return;
    }
    
    self.digitalLB.text = [NSString stringWithFormat:@"%ld",count];
    self.digitalLB.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0,25);
    
    [UIView animateWithDuration:0.15 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
          initialSpringVelocity:3.0 // 初始速度
                        options:UIViewAnimationOptionCurveLinear // 动画过渡效果
                     animations:^{
                         self.digitalLB.transform = CGAffineTransformIdentity;
                     } completion:^(BOOL finished) {
                         //递归调用，直到计时为零
                         if (type == DisplayUnitType_MiddelAnimal) return; //动画运行到中间
                         [UIView animateWithDuration:0.15 delay:0.7 usingSpringWithDamping:1.0 initialSpringVelocity:3.0 options:UIViewAnimationOptionCurveLinear animations:^{
                             self.digitalLB.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0,-35);
                         } completion:^(BOOL finished){
                         }];
                     }];
    
}


-(void)originDigitalLBWithCount:(NSInteger)count type:(DisplayUnitType)type{
    
    self.digitalLB.text = [NSString stringWithFormat:@"%ld",count];
    self.digitalLB.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0,25);
    
    [UIView animateWithDuration:0.0035 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
          initialSpringVelocity:3.0 // 初始速度
                        options:UIViewAnimationOptionCurveLinear // 动画过渡效果
                     animations:^{
                         self.digitalLB.transform = CGAffineTransformIdentity;
                     } completion:^(BOOL finished) {
                         //递归调用，直到计时为零
                         if (type == DisplayUnitType_MiddelAnimal) return;
                         
                         [UIView animateWithDuration:0.00035 delay:0.003 usingSpringWithDamping:1.0 initialSpringVelocity:3.0 options:UIViewAnimationOptionCurveLinear animations:^{
                             self.digitalLB.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0,-35);
                         } completion:^(BOOL finished){
                         }];
                     }];
    
}

-(void)clearZero
{
    self.digitalLB.transform = CGAffineTransformIdentity;
    self.digitalLB.text = @"0";
}

@end
